<template>
  <div class="app-container">
    <Find @findClicked="findClicked" @Launchform="Launchform5" @clickNumber="clickNumber1" :OriginalFilter="{ static: true }" :formList="formList" />
    <Find
      :searchImg="false"
      @findClicked="findClicked1"
      @Launchform="Launchform6"
      @clickNumber="clickNumber1"
      :OriginalFilter="{ static: true }"
      :formList="formList1"
    />
    <div>
      <el-card shadow="never" :body-style="{ padding: '11px 20px' }" class="mt10">
        <div>
          <span style="font-weight: 600">外发工艺单价 </span>
        </div>
        <Table
          @inputChange="checkboxChange3"
          @currentRow="clicktogetdata"
          @selectList="selectList5"
          :summaryData="summaryData"
          summary
          :isLoading="isLoading"
          :tableHeader="tableHeader"
          :tableData="OutgoingProcessInquiryTableDate"
          selection
        />

        <PageModule :PageData="{ buttonS: true, total }" @pageChange="pageChange" @pageSizeChange="pageSizeChange" />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'OutgoingUnitPriceFile'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import {
  OutgoingProcessInquiry,
  ProcessInquiry,
  FullCodeUsageCheck,
  deleteProcess,
  FetchInstructionOutgoingItem,
  FetchInstructionOutgoingItem1,
  InsertVendor
} from '@/api/ControlDepartment/MaterialManagement/OutgoingUnitPriceFile'
import { VendorQuery } from '@/api/WarehouseDepartment/FactoryStorage/FitOutsourcingProcessing'
import fileRequest from '@/utils/fileRequest'
import SynchronizationLoginWork from '@/utils/SynchronizationLoginWork'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, onMounted } from 'vue'
const findClicked = async (row: string) => {
  switch (row) {
    case '打勾记录检核': {
      CounterCheckParms.value = 0
      CounterCheckMethods()
      break
    }
    case '取消检核': {
      CounterCheckParms.value = 1
      CounterCheckMethods()
      break
    }
    case '置空': {
      claerInputValueAndSelectValue1()
      break
    }
    case '工艺删除': {
      handleCloseMateriaLKlc5()
      break
    }
    case '取指令外发项目': {
      if (FetchInstructionOutgoingItemParms.value.lotNo === '') {
        JMessage.warning('请输入指令')
      } else {
        FetchInstructionOutgoingItemMethods()
      }

      break
    }
    case '取外发工艺资料': {
      if (FetchInstructionOutgoingItemParms.value.lotNo === '') {
        JMessage.warning('请输入指令')
      } else {
        FetchInstructionOutgoingItem1Methods()
      }

      break
    }
    case '导出明细': {
      const res = await fileRequest.post<any, any>(`/puttingOut/PcOutListPriceExport`, OutgoingProcessInquiryParms.value)
      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      link.download = `外发单价建档导出${Date.now()}.xls` //自定义文件名字
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)

      break
    }
  }
}

// 修改
const checkboxChange3 = async (row: any) => {
  const { id, price, purMemo } = row

  const data = {
    id,
    price,
    purMemo
  }

  const result = await SynchronizationLoginWork.put<any, any>(`/puttingOut/ModificationOfOutsourcingUnitPrice`, data)

  if (result.code === 200) {
    OutgoingProcessInquiryMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 插入厂商
const InsertVendorMethodsParms = ref<any>([])

const VendorList = ref('')

const Launchform6 = (row: any) => {
  VendorList.value = row[0].selectValue.split(' ')[1]
}

const clickNumber2 = (row: any) => {
  switch (row) {
    case '4':
      OutgoingProcessInquiryParms.value.process1 = null
      break
    case '5':
      OutgoingProcessInquiryParms.value.process2 = 1
      break
    case '6':
      OutgoingProcessInquiryParms.value.process3 = 0
      break
  }
  OutgoingProcessInquiryMethods()
}

const InsertVendorMethods = async () => {
  if (InsertVendorMethodsParms.value.length === 0) {
    JMessage.warning('请勾选需要插入厂商的数据')
  } else if (VendorList.value === '') {
    JMessage.warning('请选择厂商')
  } else {
    InsertVendorMethodsParms.value = InsertVendorMethodsParms.value.map((item: any) => {
      item.vendor = VendorList.value
      return item
    })
    const result = await InsertVendor(InsertVendorMethodsParms.value)
    if (result.code === 200) {
      OutgoingProcessInquiryMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  }
}

const findClicked1 = (row: string) => {
  if (row === '插入厂商') {
    InsertVendorMethods()
  }
}

// 取工艺外发
const FetchInstructionOutgoingItem1Methods = async () => {
  const result = await FetchInstructionOutgoingItem1(FetchInstructionOutgoingItemParms.value)
  if (result.code === 200) {
    OutgoingProcessInquiryMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 取指令外发
const FetchInstructionOutgoingItemParms = ref({
  lotNo: ''
})

const FetchInstructionOutgoingItemMethods = async () => {
  const result = await FetchInstructionOutgoingItem(FetchInstructionOutgoingItemParms.value)
  if (result.code === 200) {
    OutgoingProcessInquiryMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const clicktogetdata = (row: any) => {
  deleteProcessParms.value = row.id
}

// 工艺删除
const deleteProcessParms = ref()

const deleteProcessMethods = async () => {
  const result = await deleteProcess(deleteProcessParms.value)
  if (result.code === 200) {
    OutgoingProcessInquiryMethods()
    deleteProcessParms.value = undefined
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const handleCloseMateriaLKlc5 = () => {
  if (deleteProcessParms.value === undefined) {
    JMessage.warning('请选择需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除数据吗?')
      .then(async () => {
        deleteProcessMethods()
      })
      .catch(() => {})
  }
}

const selectList5 = (row: any) => {
  CounterCheckParms1.value = row.map((item: any) => {
    return item
  })
  InsertVendorMethodsParms.value = row.map((item: any) => {
    const { id, vendor, price, flag } = item
    let newFlag

    if (item.cfmFlag === null) {
      item.flag = 1
    } else {
      item.flag = 0
    }
    return { id, vendor, price, flag: item.flag }
  })
}

// 检核
const CounterCheckParms1 = ref([])

const CounterCheckParms = ref()

const CounterCheckMethods = async () => {
  if (CounterCheckParms1.value.length === 0) {
    JMessage.warning('请选择需要审核的数据')
  } else {
    const result = await FullCodeUsageCheck(CounterCheckParms.value, CounterCheckParms1.value)
    if (result.code === 200) {
      OutgoingProcessInquiryMethods()
      CounterCheckParms1.value = []
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  }
}

// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}

const summaryData = ref<summaryDataType[]>([
  {
    index: 9,
    num: 0
  }
])

// 厂商查询
const VendorQueryMethods = async () => {
  let NewArray = []
  const result = await VendorQuery()
  NewArray = result.reduce((arr: any, item: any) => {
    if (item.vendorNo && item.vendorName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.vendorNo + ' ' + item.vendorName)
    }
    return arr
  }, [])
  formList.value[5].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
  formList1.value[0].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
}

// 工艺查询
const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

const ProcessInquiryMethods = async () => {
  let NewArrayList = []
  const result = await ProcessInquiry()
  NewArrayList = result.process.map((item: any) => {
    return item
  })
  formList.value[3].selectOptions = EvaluatesTheArrayReturnValue(NewArrayList)
}

// 查询
const Launchform5 = (row: any) => {
  FetchInstructionOutgoingItemParms.value.lotNo = row[0].inputValue
  OutgoingProcessInquiryParms.value.lotNo = row[0].inputValue
  OutgoingProcessInquiryParms.value.partName = row[1].inputValue
  OutgoingProcessInquiryParms.value.style = row[2].inputValue
  OutgoingProcessInquiryParms.value.startSysDate = row[3].dateValue[0]
  OutgoingProcessInquiryParms.value.endSysDate = row[3].dateValue[1]
  OutgoingProcessInquiryParms.value.process = row[4].selectValue
  OutgoingProcessInquiryParms.value.vendor = row[5].selectValue.split(' ')[1]
  console.log(row)
  if (row[9].checked) {
    OutgoingProcessInquiryParms.value.process1 = 0
  } else {
    OutgoingProcessInquiryParms.value.process1 = -1
  }

  if (row[10].checked) {
    OutgoingProcessInquiryParms.value.process2 = 0
  } else {
    OutgoingProcessInquiryParms.value.process2 = -1
  }

  if (row[11].checked) {
    OutgoingProcessInquiryParms.value.process3 = 0
  } else {
    OutgoingProcessInquiryParms.value.process3 = -1
  }

  OutgoingProcessInquiryMethods()
}

const OutgoingProcessInquiryParms = ref<any>({
  endSysDate: '',
  flag: null,
  lotNo: '',
  pageNum: 1,
  pageSize: 40,
  process: '',
  startSysDate: '',
  style: '',
  vendor: '',
  partName: '',
  process1: '',
  process2: '',
  process3: ''
})

const clickNumber1 = (count: string) => {
  switch (count) {
    case '1':
      OutgoingProcessInquiryParms.value.flag = null
      break
    case '0':
      OutgoingProcessInquiryParms.value.flag = 1
      break
    case '2':
      OutgoingProcessInquiryParms.value.flag = 0
      break
  }
  OutgoingProcessInquiryMethods()
}

const total = ref(0)

const pageChange = (val: any) => {
  OutgoingProcessInquiryParms.value.pageNum = val
  OutgoingProcessInquiryMethods()
}

const pageSizeChange = (val: any) => {
  OutgoingProcessInquiryParms.value.pageSize = val
  OutgoingProcessInquiryMethods()
}

const isLoading = ref(true)

const OutgoingProcessInquiryTableDate = ref([])

const OutgoingProcessInquiryMethods = async () => {
  isLoading.value = true
  const result = await OutgoingProcessInquiry(OutgoingProcessInquiryParms.value)
  OutgoingProcessInquiryTableDate.value = result.priceList.map((item: any) => {
    if (item.cfmFlag !== null) {
      item.backgroundColor = '#3FFF38'
    } else {
      item.change = 'Y'
      item.disableConfirm = true
    }
    return item
  })
  total.value = result.total
  summaryData.value[0].num = result.count
  isLoading.value = false
}

onMounted(() => {
  OutgoingProcessInquiryMethods()

  ProcessInquiryMethods()

  VendorQueryMethods()
})

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入部位',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'date',
    dateLabel: '建档日期',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '工艺',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'radio',
    checkboxLabel: '没单价',
    checked: true,
    label: '2'
  },
  {
    type: 'radio',
    checkboxLabel: '有单价',
    checked: false,
    label: '0'
  },
  {
    type: 'radio',
    checkboxLabel: '全部',
    checked: false,
    label: '1'
  },

  {
    type: 'checkbox',
    checkboxLabel: '冲裁工序',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '手工工序',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '外发工序',
    checked: false
  },
  {
    type: 'button',
    checkboxLabel: '导出明细'
  },
  {
    type: 'button',
    checkboxLabel: '工艺删除'
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录检核'
  },
  {
    type: 'button',
    checkboxLabel: '取消检核'
  },
  {
    type: 'button',
    checkboxLabel: '取外发工艺资料'
  },
  {
    type: 'button',
    checkboxLabel: '取指令外发项目'
  },
  {
    type: 'button',
    checkboxLabel: '置空'
  }
])

const formList1 = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '插入厂商'
  }
])

const claerInputValueAndSelectValue1 = () => {
  formList.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
    if (item.type === 'date') {
      item.dateValue = ''
    }
  })
}

const tableHeader = ref([
  {
    label: '指令',
    prop: 'lotNo',
    change: true
  },
  {
    label: '型体',
    prop: 'style',
    change: true
  },
  {
    label: '颜色',
    prop: 'color',
    change: true
  },
  {
    label: '工艺',
    prop: 'process',
    change: true
  },
  {
    label: '部位名称',
    prop: 'partName',
    backgroundColor: true,
    change: true
  },
  {
    label: '片数',
    prop: 'cutNum',
    change: true
  },
  {
    label: '建档日期',
    prop: 'purDate',
    change: true
  },
  {
    label: '厂商',
    prop: 'vendor',
    change: true
  },
  {
    label: '单价',
    prop: 'price'
  },
  {
    label: '备注',
    prop: 'purMemo'
  },
  {
    label: '检核',
    prop: 'cfmFlag',
    change: true
  }
])
</script>

<style lang="less" scoped>
:deep(.el-table .el-table__footer-wrapper) {
  --el-table-row-hover-bg-color: rgb(128, 255, 0);
}
</style>
